/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ez2find.manager;

import com.ez2find.model.Area;
import com.ez2find.model.RegisterUser;
import com.ez2find.model.Resume;
import com.ez2find.model.Site;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Owner
 */
@Stateless
public class ResumeManagerBean implements ResumeManagerLocal {
    
    @PersistenceContext
    private EntityManager em;
    
    public void create(Resume resume)throws Exception {
        em.persist(resume);
    }
 
    public void update(Resume resume)throws Exception {
        em.merge(resume);
    }
    
    public Resume merge(Resume resume)throws Exception {
        return em.merge(resume);
    }
    
    public void delete(Resume resume)throws Exception {
        resume=em.merge(resume);
        em.remove(resume);
    }
    
    public Resume find(long id){
        return em.find(Resume.class,id);
    }

    public List<Resume> listAll(){
        Query query=em.createNamedQuery("listAllResumes");
        List<Resume> result=query.getResultList();
        return result;
    }
    
    public long lastCode(){
        try{
            Query query=em.createNamedQuery("lastResumeCode");
            return (Long) query.getSingleResult();
        }catch(Exception ex){
            return 0;
        }
    }
    
    public List<Resume> listResumesSite(Site site, String consult){
        Query query=em.createQuery("SELECT r FROM Resume r WHERE r.site=:site AND r.status='active'"+consult+" ORDER BY r.publishDate DESC, r.code DESC");
        query.setParameter("site", site);
        List<Resume> result=query.getResultList();
        return result;
    }
    
    public List<Resume> listResumesArea(Area area, String consult){
        Query query=em.createQuery("SELECT r FROM Resume r WHERE r.area=:area AND r.status='active'"+consult+" ORDER BY r.publishDate DESC, r.code DESC");
        query.setParameter("area", area);
        List<Resume> result=query.getResultList();
        return result;
    }
    
}
